package com.fenghongzhang.day016;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.List;


/**
 * ${FENG}
 * 2019-07-24
 */
public class MusicAdapter extends BaseAdapter {
    private Context context;
    private List<Music> list;


    public MusicAdapter(Context context, List<Music> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {

        ViewHolder holder = null;
        if (view == null) {
            holder = new ViewHolder();
            // 引入布局
            view = View.inflate(context, R.layout.lv_layout, null);
            // 实例化对象
            holder.lv_artist = (TextView) view.findViewById(R.id.lv_artist);
            holder.lv_duration = (TextView) view.findViewById(R.id.lv_duration);
            holder.lv_img = (ImageView) view.findViewById(R.id.lv_img);
            holder.lv_title = (TextView) view.findViewById(R.id.lv_title);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }
        // 给控件赋值
        Music music = list.get(i);
        holder.lv_title.setText(music.getTitle());
        holder.lv_duration.setText(MusicUtil.fmtTime(music.getDuration())); //3:15  5:04
        holder.lv_artist.setText(music.getArtist());

        //Glide 设置专辑封面
        Glide.with(context).load(MusicUtil.getPIc(context,music.getAlbumId())).into(holder.lv_img);

        return view;
    }

    class ViewHolder {
        ImageView lv_img;//图片
        TextView lv_title;// 歌曲名
        TextView lv_artist;// 歌手
        TextView lv_duration;// 时长
    }

}
